"""
Case Type   : Compatibility
Case Name   : 验证prepare语句的嵌套;部分测试点合理报错
Create At   : 2024/10/16
Owner       : liu-tong-8848
Description :
              1.创建prepare语句并执行
              2.创建prepare语句;使用其他语句的stmt_name作为preparable_stmt;
              3.创建prepare语句;自己的stmt_name作为preparable_stmt;
              4.创建prepare语句;preparable_stmt语句中存在引号;
              5.清理环境
Expect      :
              1.成功
              2.合理报错
              3.合理报错
              4.合理报错
              5.清理环境成功
History     :
"""

import os
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node


class SQL(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.Constant = Constant()
        self.primary_sh = CommonSH('PrimaryDbBmode')
        self.db_user_node = Node(node='PrimaryDbBmode')

    def test_add_dimension(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        step1 = '--step1:创建prepare语句并执行;expect:成功'
        self.log.info(step1)
        sql = "prepare test_prepare_004a from 'select 1'; execute test_prepare_004a; "
        self.log.info(sql)
        result = self.primary_sh.execut_db_sql(sql)
        self.log.info(result)
        self.assertIn("PREPARE", result, "执行失败" + step1)
        self.assertNotIn("ERROR", result, "执行失败" + step1)

        step2 = '--step2:创建prepare语句;使用其他语句的stmt_name作为preparable_stmt;expect:合理报错'
        self.log.info(step2)
        sql = "prepare test_prepare_004a from 'select 1'; prepare test_prepare_004b from 'test_prepare_004a'; "
        self.log.info(sql)
        result = self.primary_sh.execut_db_sql(sql)
        self.log.info(result)
        self.assertIn("ERROR:  syntax error", result, "执行失败" + step2)
        sql = "prepare test_prepare_004a from 'select 1'; prepare test_prepare_004b from 'execute test_prepare_004a'; "
        self.log.info(sql)
        result = self.primary_sh.execut_db_sql(sql)
        self.log.info(result)
        self.assertIn("ERROR:  utility statements cannot be prepared", result, "执行失败" + step2)
        sql = "prepare test_prepare_004a from 'select 1'; prepare test_prepare_004b " \
              + "from 'deallocate prepare test_prepare_004a';"
        self.log.info(sql)
        result = self.primary_sh.execut_db_sql(sql)
        self.log.info(result)
        self.assertIn("ERROR:  utility statements cannot be prepared", result, "执行失败" + step2)

        step3 = '--step3:创建prepare语句;自己的stmt_name作为preparable_stmt;expect:合理报错'
        self.log.info(step3)
        sql = "prepare test_prepare_004a from 'select 1'; prepare test_prepare_004a from 'test_prepare_004a'; "
        self.log.info(sql)
        result = self.primary_sh.execut_db_sql(sql)
        self.log.info(result)
        self.assertIn("ERROR:  syntax error", result, "执行失败" + step3)
        sql = "prepare test_prepare_004a from 'select 1'; prepare test_prepare_004a from 'execute test_prepare_004a'; "
        self.log.info(sql)
        result = self.primary_sh.execut_db_sql(sql)
        self.log.info(result)
        self.assertIn("ERROR:  utility statements cannot be prepared", result, "执行失败" + step3)
        sql = "prepare test_prepare_004a from 'select 1'; prepare test_prepare_004a " \
              + "from 'deallocate prepare test_prepare_004a';"
        self.log.info(sql)
        result = self.primary_sh.execut_db_sql(sql)
        self.log.info(result)
        self.assertIn("ERROR:  utility statements cannot be prepared", result, "执行失败" + step3)

        step4 = '--step4:创建prepare语句;preparable_stmt语句中存在引号;expect:合理报错'
        self.log.info(step4)
        sql = "prepare test_prepare_004c from 'select from_base64('MQ==')';"
        self.log.info(sql)
        result = self.primary_sh.execut_db_sql(sql)
        self.log.info(result)
        self.assertIn("ERROR:  syntax error", result, "执行失败" + step4)
        sql = " prepare test_prepare_004c from 'select from_base64(\"MQ==\")';"
        self.log.info(sql)
        result = self.primary_sh.execut_db_sql(sql)
        self.log.info(result)
        self.assertIn("ERROR", result, "执行失败" + step4)

    def tearDown(self):
        step5 = 'step5:清理环境 expect:清理环境成功'
        self.log.info(step5)
        clean_sql = "deallocate prepare test_prepare_004a;"
        self.primary_sh.execut_db_sql(clean_sql)
        self.log.info("prepare语句无法跨会话生效，由于自动化用例会跨会话执行这些语句，所以自动化结束时无需清理")
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
